#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：zhcx-service-test 
@File    ：invoking_interface.py
@Author  ：关依林
@Date    ：2023/7/30 16:06 
'''
import datetime
import json
import requests

def getAmazonListingData(field: list, sku: list):
    """

    @param field: 条件
    @param sku: 指定sku
    @return:
    """
    url = "http://api-msku.zhcxkj.com/ordersource/product/getOrderSourceSkuInfo"
    payload = json.dumps(
        {
            "source": field,
            "input":
                {
                    "CustomerID": 1,
                    "SKUs": sku
                }
        }
    )
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Basic YXBpdXNlcjpzMG9rLV9rI3NWdmFVaQ=='
    }

    response = requests.request("POST", url, headers=headers, data=payload).json()
    return response

def getTimedTask(jobId):
    """

    @param jobId:
    @return:
    """
    currentTime = datetime.datetime.now().strftime("%Y-%m-%d")
    endTime = (datetime.datetime.now() + datetime.timedelta(days=-7)).strftime("%Y-%m-%d")
    url = "http://whaleapi.internal.zhcxkj.com/xxl-job-admin/joblog/pageList"
    payload = f"jobGroup=48&jobId={jobId}&logStatus=1&filterTime={endTime}+00%3A00%3A00+-+{currentTime}+23%3A59%3A59&start=0&length=10"
    headers = {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'cookie': '_ati=499110749279; _ga=GA1.2.1932834896.1665385001; XXL_JOB_LOGIN_IDENTITY=7b226964223a312c22757365726e616d65223a2261646d696e222c2270617373776f7264223a226531306164633339343962613539616262653536653035376632306638383365222c22726f6c65223a312c227065726d697373696f6e223a6e756c6c7d'
    }
    response = requests.request("POST", url, headers=headers, data=payload).json()["data"]
    startTime = response[1]["triggerTime"]
    endTime = response[0]["triggerTime"]
    return startTime, endTime

def getIncrementChangeData(startTime, endTime):
    """

    @param startTime:
    @param endTime:
    @return:
    """
    url = "http://erpapi.zhcxkj.com/erp/products/getdatamonitor"
    payload = json.dumps({
        "customerId": 1,
        "skus": [],
        "fieldTypes": [2, 15, 16, 18, 19, 20],
        "startTime": startTime,
        "endTime": endTime,
        "currentPage": 1,
        "pageSize": 100000
    })
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Basic YXBpdXNlcjpFN0kxLWxJX2QjOGRscw=='
    }

    response = requests.request("POST", url, headers=headers, data=payload).json()
    return response


def getStrategyDetailAPIData(sourceId, strategyId, appointType, appointSkuSet):
    import requests
    import json

    url = "https://webapi.internal.zhcxkj.com/open/listingStrategy/pageStrategyAmazonListingByCursor"

    payload = json.dumps({
        "appointSkuSet": appointSkuSet,
        "appointType": appointType,
        "pageIndex": 1,
        "importDate": "",
        "pageSize": 1000,
        "sourceIdSet": [sourceId],
        "strategyId": strategyId
    })
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJyYW5kb21udW1iZXIiOiIzZjczYWU2Ni0zMTg1LTQ3NWMtODk4OC1jNDAzOTI3NzI3YWMiLCJ1c2VyX2lkIjoyLCJ1c2VyX2tleSI6ImVlOTFiNDE5ZDYwNWIzZGFlYzk3NTIyZWM5N2E3YzQ2IiwidXNlcm5hbWUiOiLlvpDnm5vmnb4ifQ.hzd9_Ee2x5-42hSr-hWQ1qwbwGNHdM61ZIYRb6sie7cbCeaHUvGqtxjtOBgstvZEGSw_12Cor2HGU4kbJGOtrw'
    }

    response = requests.request("POST", url, headers=headers, data=payload).json()

    print(response.text)
